<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="ppqCreation"><i class="el-icon-plus"></i>品牌现金券</el-button>
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="分公司">
        <el-select v-model="formInline.branchId" v-if="isBranch" style="width: 180px" @change="getDeptList" @visible-change="visibleChange" filterable>
          <el-option
            v-for="item in branchList"
            :label="item.branch_Name"
            :value="item.branch_Id">
          </el-option>
        </el-select>
        <el-input v-if="!isBranch" v-model="branchName" disabled></el-input>
      </el-form-item>
      <!--<el-form-item label="所属部门">-->
        <!--<el-select v-model="formInline.deptId" v-if="isBranch" style="width: 180px" filterable>-->
          <!--<el-option-->
            <!--v-for="item in deptList"-->
            <!--:value="item.org_id"-->
            <!--:label="item.org_name">-->
          <!--</el-option>-->
        <!--</el-select>-->
        <!--<el-input v-if="!isBranch" v-model="orgName" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="分公司">-->
        <!--<el-input v-model="this.branchName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属部门">-->
        <!--<el-input v-model="this.orgName" style="width: 220px" disabled></el-input>-->
      <!--</el-form-item>-->
      <el-form-item label="创建周期">
        <el-date-picker
          :editable="false"
          v-model="formInline.time"
          type="daterange"
          value-format="yyyy-MM-dd"
          @change="timeChange"
          placeholder="选择日期范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="优惠券名称">
        <el-input v-model="formInline.tname"></el-input>
      </el-form-item>
      <el-form-item label="ID">
        <el-input v-model="formInline.id"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%"
      border>
      <!--<el-table-column-->
        <!--show-overflow-tooltip-->
        <!--prop="depatmentName"-->
        <!--label="所属部门"-->
        <!--align="center"-->
        <!--width="120">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="id"
        label="优惠券ID"
        align="center"
        width="80">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="tname"
        label="优惠券名称"
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="tcost"
        label="面值"
        width="100"
        align="center">
      </el-table-column>
      <el-table-column
        prop="fulluse"
        label="满多少可用"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="productNum"
        label="商品数"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="createUser"
        label="创建人"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="创建时间"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="remark"
        label="备注"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="100" >
        <template scope="scope">
          <el-button @click="detail(scope.$index, scope.row)" size="small">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(pageData.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <!--详情弹窗-->
    <el-dialog title="品牌现金券详情" :visible.sync="detailDialog" top="10%" class="detail-info">
      <div class="form-item" title="aa">
        <pre style="margin: -10px;line-height: 28px">
          分公司: {{detailData.branchName}}
          所属部门：{{detailData.departmentName}}
          优惠券名称：{{detailData.tname}}
          金额：{{detailData.tcost}}
          满多少：{{detailData.fullUse}}
          参与类型：<span v-if="detailData.brandId">品牌--{{detailData.brandName}}</span><span v-else>商品</span>
          Logo选择：
          <img :src="this.imgHost + '/' + detailData.imgUrl" width="220px" height="220" alt="">
        </pre>
      </div>
      <div v-show="!detailData.brandId" class="form-item">
        <h4>商品信息</h4>
        <el-table
          :data="detailData.products"
          style="width: 100%"
          border>
          <el-table-column
            prop="hmpId"
            label="商品ID"
            width="100">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpName"
            min-width="100"
            label="商品名称">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpUnit"
            min-width="100"
            label="单位">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpNorms"
            min-width="100"
            label="规格">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="hmpBuyPrice"
            min-width="100"
            label="系统售价">
          </el-table-column>
        </el-table>
      </div>
      <div class="form-item">
        <h4>备注</h4>
        <el-form label-width="180px">
          <el-input  type="textarea" placeholder="请设置活动说明" v-model="detailData.remark" style="width: 500px"></el-input>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { apiHost } from '../../../../../config/settings.js'
  import { getInfo, activity } from '../../../../../config/proxy.js'
//  import { mapState } from 'vuex'
  import mixins from '../../../../../components/mixins.vue'
  export default {
    mixins: [mixins],
    data () {
      return {
        deptList: [],
        visible: false,
        imgHost: '',
        formInline: {
          branchId: '',
          deptId: '',
          id: '',
          time: '',
          tname: ''
        },
        tableData: [],
        isLoading: false,
        pageData: {
          pageSize: 10,
          page: 1
        },
        total: '',
//      详情弹窗
        detailDialog: false,
        detailData: {}
      }
    },
    methods: {
      getDeptList (val) {
        if (this.visible === true) {
          this.formInline.deptId = ''
        }
        var _data = {}
        this.branchList.forEach(item => {
          if (val === item.branch_Id) {
            _data.orgId = item.org_id
          }
        })
        getInfo.department(_data, (res) => {
          if (res.message === 'ok') {
            if (val === 1) {
              this.deptList = []
              res.content.forEach(element => {
                if (element.org_name.indexOf('公司') === -1) {
                  this.deptList.push(element)
                }
              })
              this.matchDept()
            } else {
              this.deptList = res.content
              this.matchDept()
            }
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error'
            })
          }
        })
      },  // 获取部门
      matchDept () { // 匹配 当前登录人的部门与部门列表
        for (let index = 0; index < this.deptList.length; index++) {
          const element = this.deptList[index]
          if (Number(this.formInline.deptId) === Number(element.org_id)) {
            return
          } else {
            if (index === this.deptList.length - 1) {
              this.formInline.deptId = ''
            }
          }
        }
      },
      visibleChange (val) {
        this.visible = val
      },
      ppqCreation () {
        this.openNewWindow('ppq_add', '新增品牌券', 'ppq_add')
      },
      timeChange (val) {
        this.formInline.beginDate = val[0]
        this.formInline.endDate = val[1]
      },
      onSubmit () {
        this.isLoading = true
        var _data = Object.assign(this.formInline, this.pageData)
        activity.yhqhd.jcq.ppq_list(_data, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.tableData = res.content.content
            this.total = res.content.count
            this.pageData.page = res.content.page
            this.isLoading = false
          } else {
            this.$message({
              showClose: true,
              message: '查询失败',
              type: 'error'
            })
          }
        })
      },
      handleCurrentChange (val) {
        this.pageData.page = val
        this.onSubmit()
      },
      detail (index, row) {
        this.detailDialog = true
        activity.yhqhd.jcq.ppq_detail({id: row.id}, (res) => {
          console.log(res)
          if (res.message === 'ok') {
            this.detailData = res.content
            this.detailDialog = true
          } else {
            this.$message({
              showClose: true,
              message: '查看详情失败',
              type: 'error'
            })
          }
        })
      }
    },
    computed: {
      orgName: function (state) {
        return this.$store.state.user.info.parOrgName
      },
      orgId: function (state) {
        return this.$store.state.user.info.or_org_id
      },
      branchName: function (state) {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function (state) {
        return this.$store.state.user.info.branchId
      },
      update_state: function () {
        return this.$store.state.updateState.ppqState
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.onSubmit()
      },
      branchList (a, b) {
        this.getDeptList(this.formInline.branchId)
      }
    },
    created () {
      this.formInline.branchId = this.branchId
//      this.formInline.deptId = this.orgId
      this.imgHost = apiHost.imgHost
    }
  }
</script>
<style>
</style>
